package org.zjt.demo.common;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zjt.demo.util.CommonUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by zhang on 2016/12/12.
 * 处理跨域的问题，利用修改http头的方法
 *
 */
public class ProtectorFilter implements javax.servlet.Filter {
    private static final Logger LOG= LoggerFactory.getLogger(ProtectorFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOG.info("初始化参数:{}",JSONObject.toJSONString(filterConfig.getInitParameterNames()));
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        LOG.info(String.format("处理XSS、SQL、CSRF、File安全请求 ip:%s\t\tparams:%s", CommonUtil.getRemoteHost((HttpServletRequest) request),JSONObject.toJSONString(request.getParameterMap())));

        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
